/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月3日
 */
package com.tank.game.api.config;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 提供基于某种名称前缀的子初始化参数
 *
 * @author Zed
 */
public final class SubsetParameters implements Parameters {

    private final String prefix;
    private final Parameters initParams;

    /**
     * 构造方法
     *
     * @param initParams {@link Parameters}, 初始化参数
     * @param prefix     {@link String}, 前缀
     */
    public SubsetParameters(Parameters initParams, String prefix) {
        this.initParams = Objects.requireNonNull(initParams, "\"initParams\" cannot be null.");
        this.prefix = prefix.endsWith(DOT) ?
                prefix :
                prefix.concat(DOT);
    }

    /**
     * 获取前缀
     *
     * @return {@link String}
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * 根据<code>name</code>获取参数值
     *
     * @param name {@link String}, 参数名
     * @return {@link String}, 参数值，参数不存在则返回null.
     */
    @Override
    public String getParameter(String name) {
        String fullName = this.prefix.concat(name);
        return this.initParams.getParameter(fullName);
    }

    /**
     * 获取所有初始化参数名称
     *
     * @return {@link List}, 列表
     */
    @Override
    public List<String> getParameterNames() {
        List<String> result = this.initParams.getParameterNames(this.prefix);
        return result.stream()
                .map(name -> name.substring(this.prefix.length()))
                .collect(Collectors.toList());
    }

    /**
     * 获取所有满足<code>prefix</code>前缀的参数名<br<
     * 如果<code>name</code>不是以"."作为结尾，方法执行时会自动添加"."后再进行比较。
     *
     * @param prefix {@link String}
     * @return {@link List}
     */
    @Override
    public List<String> getParameterNames(String prefix) {
        final String fullPrefix = prefix.endsWith(DOT) ?
                this.prefix.concat(prefix) :
                this.prefix.concat(prefix).concat(DOT);
        List<String> result = this.initParams.getParameterNames(fullPrefix);
        return result.stream()
                .map(name -> name.substring(this.prefix.length()))
                .collect(Collectors.toList());
    }

    @Override
    public String toString() {
        return "SubParameters{" +
                "prefix='" + prefix + '\'' +
                ", initParams=" + initParams +
                '}';
    }
}
